*--- 1. Match original sale with Land Reg (only new properties sold after 2012)

use "${datadir}\HTB_main", replace

replace purchaseprice = purchaseprice * 1000 // for matching
bysort pcd purchaseprice date (psd_id): keep if _n == 1 // remove duplicates before matching

joinby pcd purchaseprice using ///
	"${datadir}\landreg_sel_new_no_Wales_post2012" 
	
// unique schemeid
gen diff_date = dated - date
bysort psd_id: egen mindiff = min(abs(diff_date))
keep if abs(diff_date) == mindiff

// unique unit_id dated
bysort unit_id date: egen mindiff2 = min(abs(diff_date))
keep if abs(diff_date) == mindiff2  

sum diff_date, det
keep if inrange(diff_date, r(p1), r(p99))

bysort psd_id (unit_id dated): keep if _n == 1
bysort unit_id dated (psd_id): keep if _n == 1

keep unit_id dated psd_id
save "${datadir}\landreg_psd_matched_first_sale_new", replace



*--- 2. Match again to find which ones have a next sale in Land Reg

// match again
merge 1:m unit_id dated using "${datadir}\landreg_selected_no_Wales_prevnew"  // all unit_id that start as new


// keep only the unit_id who matched
bysort unit_id: egen merged = max(psd_id)
keep if merged != .
drop psd_id
rename merged psd_id


// get rid of duplicates
bysort unit_id dated (new lease price): keep if _n == _N  

// keep only those who resold
bysort unit_id (dated): gen n = _n
keep if n == 2

ren dated dated_next
keep psd_id dated_next
save "${datadir}\landreg_matched_htb_main_resold", replace
